﻿//Copyright 2008 Kristian Gundry And Rune Juhl-Petersen
//This file is part of Plain.

//Plain is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.

//Plain is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public License
//along with Plain.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Plain.Search;
using Plain.DuckTyping;

namespace Plain.Data {
    public static class IDaoExtensions {
        #region Methods

        /// <summary>
        /// Persists the entity.
        /// </summary>
        /// <remarks>
        /// If <see cref="IDao"/> cannot be found, it will throw an <see cref="Exception"/>
        /// </remarks>
        /// <param name="entity">the entity to persist</param>
        /// <returns>True/false</returns>
        public static bool Save<T>(this IBusinessEntity<T> entity) {
            IDaoFactory factory = PlainApplication.Config.DaoFactory;
            object dao = factory.GetMethod<object>("GetDao", entity.GetType(), typeof(T)).Call();
            dao.CallMethod("Save", entity);
            return true;
        }
        //public static bool Save1<T>(this IBusinessEntity<T> entity) {
        //    IDaoFactory factory = PlainApplication.Config.DaoFactory;
        //    dynamic dao = factory.GetMethod<object>("GetDao", entity.GetType(), typeof(T)).Call();
        //    //object dao = factory.GetMethod<object>("GetDao", entity.GetType(), entity.GetType().GetProperty("Id").PropertyType).Call();
        //    dao.Save(entity);
        //    //dao.CallMethod("Save", entity);
        //    return true;
        //}
        /// <summary>
        /// Deletes the entity from persistance media.
        /// </summary>
        /// <remarks>
        /// If <see cref="IDao"/> cannot be found, it will throw an <see cref="Exception"/>
        /// </remarks>
        /// <param name="entity">The entity to delete</param>
        /// <returns>True/False</returns>
        public static bool Delete<T>(this IBusinessEntity<T> entity) {
            IDaoFactory factory = PlainApplication.Config.DaoFactory;
            object dao = factory.GetMethod<object>("GetDao", entity.GetType(), entity.GetType().GetProperty("Id").PropertyType).Call();
            dao.CallMethod("Delete", entity);
            return true;
        }

        #endregion
    }
    public static class SearchExtension {
        public static IList<T> Search<T, TT>(this ISearchableDao<T, TT> dao, Criteria crit) where T : IBusinessEntity<TT> {
            return dao.Search(crit, new Dictionary<string, object>(), 0, long.MaxValue, "");
        }

        public static IList<T> Search<T, TT>(this ISearchableDao<T, TT> dao, Criteria crit, string order) where T : IBusinessEntity<TT> {
            return dao.Search(crit, new Dictionary<string, object>(), 0, long.MaxValue, order);
        }
        public static IList<T> Search<T, TT>(this ISearchableDao<T, TT> dao, Criteria crit, IDictionary<string, object> dic) where T : IBusinessEntity<TT> {
            return dao.Search(crit, dic, 0, long.MaxValue, "");
        }
        public static IList<T> Search<T, TT>(this ISearchableDao<T, TT> dao, Criteria crit, IDictionary<string, object> dic, string order) where T : IBusinessEntity<TT> {
            return dao.Search(crit, dic, 0, long.MaxValue, order);
        }
        public static int GetCount<T, TT>(this ISearchableDao<T, TT> dao, Criteria crit) where T : IBusinessEntity<TT> {
            return dao.GetCount(crit, new Dictionary<string, object>());
        }
		
    }
}
